A First-Principles-Driven Life

Posts tagged " ":

07 Jul 2023

A Solidity Compilers Collection in Nix

I have written solc.nix for a while, this article is a summary of it.

It is a collection of solidity compilers as nix expressions. While most solidity development toolchains (foundry, truffle, hardhat, etc.) have their bespoke mechanisms of downloading solidity compiler (solc) binaries, there are situations you would want not to rely on them:

If any of these cases apply to you, you should consider using solc.nix. Here are some tips on how.

Install Nix

Here is a write-up of why I think you should use Nix.

The fastest way to have it installed on your system is to follow the official instruction.

Once installed, you are good to go!

Using Nix Shell

If you just wanted to have some version of solc ad-hoc, you just need one simple line:

$ nix shell github:hellwolf/solc.nix#solc_0_4_26 github:hellwolf/solc.nix#solc_0_8_19

Then you will have two different solc binary available for you:

$ solc-0.4.26 --version
solc, the solidity compiler commandline interface
Version: 0.4.26+commit.4563c3fc.Linux.g++

$ solc-0.8.19 --version
solc, the solidity compiler commandline interface
Version: 0.8.19+commit.7dd6d404.Linux.g++

Using Nix Flake

If you want a deeper integration of it, you should consider using Nix Flake, here is an example of how to have multiple solc installed and one of them as the default one:

{
  inputs = {
    solc = {
      url = "github:hellwolf/solc.nix";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, solc }: let
    pkgs = import nixpkgs {
      system = "x86_64-linux";
      overlays = [
        solc.overlay
      ];
    };
  in {
    devShell.x86_64-linux = with pkgs; mkShell {
      buildInputs = [
        solc_0_4_26
        solc_0_7_6
        solc_0_8_19
        (solc.mkDefault pkgs solc_0_8_19)
      ];
    };
  };
}

Contribute

If you find this tool useful, please follow up at the git repo, contribute to keep this up to date, and share with people you might think it can help them too!

Tags: nix solidity
11 Dec 2022

Stop Writing Instructions and Use Nix

If you are still writing development environment setup instruction for your project, time to use Nix!

With Nix:

More over, you should use Nix Flakes. This upcoming new feature of Nix will power-up your build environment with a lock file that is similar to your yarn.lock/package-json.lock/Cargo.lock/etc., but for all types of projects.

It is a experience changer for many engineering organizations, e.g. at shopify.

Personally, each time I want to contribute to an open source project, the first thing I would do is to add a flake.nix to their project and decorate their CONTRIBUTING.md with a much more concise development environment setup using Nix section.

For more reasons why you should use Nix, I recommend these readings:

After you are "nix pilled" inevitably, Nix official website has a pretty decent collection of learning materials from getting started to comprehensive references.

There are also some good materials done by the community:

Further more, Nix Flakes have also its own interesting materials to read:

Happy Nix!

Tags: nix devops
Other posts
Creative Commons License
A First-Principles-Driven Life by Miao, ZhiCheng is licensed under a Creative Commons Attribution 3.0 License.